from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os

from html import CheckBox, DropDown #importing the html wrappers
import validate #import validation
import cgi #for escaping html chars in forms
from cStringIO import StringIO

import unittest
import sys

def runTestGetResult():
    result = StringIO()
    from unitTests import UnitTests
    suite = unittest.TestLoader().loadTestsFromTestCase(UnitTests)
    unittest.TextTestRunner(verbosity=2, stream=result).run(suite)
    result = result.getvalue()
    return result

class WebTest(webapp.RequestHandler):
    """
    Runs unitTests over web.
    """
    
    def get(self):
        """
        Calls renderTemplate() for this class.
        """
        self.renderTemplate(testOutput = runTestGetResult())
        
    def post(self):
        self.get()
        
    def renderTemplate(self, testOutput) :
        """
        Renders the Template classes.html for this class.
        """
        
        #setting up template dictionary
        templateValues = {
            'testOutput': testOutput.split('\n'),
        }

        #telling it to use the template
        path = os.path.join(os.path.dirname(__file__), 'webTest.html')
        self.response.out.write(template.render(path, templateValues))

application = webapp.WSGIApplication([('/webTest', WebTest)], debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == "__main__":
    main()